package com.opensource.component.swagger.controller;

import com.google.common.collect.Lists;
import com.opensource.component.swagger.config.SwaggerConfig;
import com.opensource.component.swagger.config.SwaggerConfig.DocketInfo;
import java.util.HashMap;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@Slf4j
@RestController
public class DocController {

  @Bean("lb")
  private RestTemplate getRestTemplate() {
    return new RestTemplate();
  }

  @Autowired
  private SwaggerConfig config;

  @GetMapping(value = "doc/{service}", produces = {"application/json"})
  public String getDoc(@PathVariable String service,
      @CookieValue(name = "yht_access_token", required = false) String yhtAccesstoken) {
    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(Lists.newArrayList(MediaType.APPLICATION_JSON));
    HttpEntity<String> request = new HttpEntity<>(headers);

    DocketInfo docketInfo = config.getDocket().get(service);
    final String url = docketInfo.getLocation();
    ResponseEntity<String> responseEntity = new RestTemplate().exchange(url, HttpMethod.GET,
        request,
        String.class);
    return responseEntity.getBody();
  }


//  private String getYbToken() {
//    HttpHeaders headers = new HttpHeaders();
//    headers.setAccept(Lists.newArrayList(MediaType.APPLICATION_JSON));
//    headers.set(JwtUtils.HEADER_SIGN, JwtInnerUtils.sign(new HashMap<>()));
//    HttpEntity<String> request = new HttpEntity<>("mdhpm1a4", headers);
//
//    ResponseEntity<String> responseEntity = new RestTemplate().exchange(
//        "https://bip-daily.yyuap.com/tax/cas/v1/privateApi/iuap/access-token/corpid",
//        HttpMethod.POST, request, String.class);
//    log.info(responseEntity.getBody());
//    Map<String, String> datas = new GsonBuilder().create()
//        .fromJson(responseEntity.getBody(), Map.class);
//    return datas.get("datas");
//  }
}
